package com.baidu.kshpt.controller;

import com.baidu.kshpt.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/test")
@Api("测试接口")
@RestController
public class TestController {
    @Autowired
    private TestService testService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ApiOperation("测试方法")
    public Object test() {

        System.out.println("呵呵");

        Object jj = redisTemplate.opsForValue().get("jj");
        if (ObjectUtils.isEmpty(jj)) {
            List<Map<String, Object>> result = testService.getListTest();
            redisTemplate.opsForValue().set("jj", result,20000);
        } else {
            redisTemplate.delete("jj");
            return jj;
        }
        return "result";
    }

    @RequestMapping(value = "/testmq", method = RequestMethod.GET)
    @ApiOperation("测试mq生产者")
    public String testmq() {
        for (int i = 1; i < 10; i++) {
            Map<String, Object> ii = new HashMap<>();
            ii.put("int", "int:" + i);
            ii.put("String", "String:" + i);
            rabbitTemplate.convertAndSend("testDirectExchange", "TestDirectRouting", ii);
        }
        return "send success";
    }
}
